const express = require("express")
const router = express.Router()

const rolesController = require("../controllers/rolesController")
const { body, query } = require('express-validator');

// router.use(function timeLog(req, res, next) {
//     console.log('Time: ', Date.now())
//     //模块路由中检测是否携带了 token
//     if (!req.auth) {
//         return res.send({
//             status: 403,
//             message: "缺少token"
//         });
//     }
//     next()
// })
// 引入中间件
const checkPermission = require('../middleware/checkPermission');
const auth = require('../middleware/auth'); ''
// 1. 角色的添加
router.post("/addrole", auth, checkPermission("system_role_add"), rolesController.addrole)
// 2. 角色的软删除
router.get("/del_f", auth, checkPermission("system_role_delete"), rolesController.deleterole_f)
router.get("/start", auth, checkPermission("system_role_delete"), rolesController.start)
// 3. 角色的真删除
router.get("/del_t", auth, checkPermission("system_role_delete"), rolesController.deleterole_t)
// 4. 角色的列表
router.get("/list",
    [
        query('page_size').optional().toInt(),
        query('page').optional().toInt()
    ], auth, checkPermission("system_role_list"),
    rolesController.role_list_c)
// 5. 角色的修改获取数据
router.get("/edit_get", auth, checkPermission("system_role_list"), rolesController.edit_get_c)
router.get("/per_view", auth, checkPermission("system_role_list"), rolesController.permission_view)
// 6. 角色的修改设置数据
router.post("/edit_set", [
    body('rolecode').notEmpty().withMessage('角色编码不能为空'),
    body('name').isLength({ min: 2 }).withMessage('角色描述至少需要2个字符'),
    body('description').isLength({ min: 5 }).withMessage('角色描述至少需要5个字符')
], auth, checkPermission("system_role_edit"),
    rolesController.edit_set_c)
// 7. 角色权限设置
router.get("/set_permission", auth, checkPermission("system_role_set"), rolesController.expression_set_c)
// 8. 全部角色
router.get("/allrole", auth, rolesController.all_role)
module.exports = router;